﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZSZ.DTO;
using ZSZ.IService;
using ZSZ.Model;

namespace ZSZ.Service
{
    public partial class RegionService : BaseService<Region>, IRegionService
    {
        private IQueryable<Region> GetJoinAll()
        {
            return GetAll().Include(r => r.City).AsNoTracking();
        }

        /// <summary>
        /// 获取区域列表
        /// </summary>
        /// <param name="options"></param>
        /// <param name="total"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<RegionDTO> GetPagedData(RegionSearchOptions options, out long total, int pageIndex = 1, int pageSize = 10)
        {
            IQueryable<Region> entities = GetJoinAll();
            if (options.SelectCity != null)
            {
                entities = entities.Where(r => r.CityId== options.SelectCity);
            }
            if (!string.IsNullOrEmpty(options.Keywords))
            {
                entities = entities.Where(r => r.Name.Contains(options.Keywords));
            }

            return CurrentDal.GetPagedData(entities, out total, pageIndex, pageSize).ToList().Select(r => ToDTO(r)).ToList();
        }

        /// <summary>
        /// 根据城市获取区域
        /// </summary>
        /// <param name="cityId"></param>
        /// <returns></returns>
        public RegionDTO[] GetByCityId(long cityId)
        {
            return GetJoinAll().Where(r => r.CityId == cityId).ToList().Select(r => ToDTO(r)).ToArray();
        }


        public RegionDTO GetDTOById(long id)
        {
            var regions = GetJoinAll().Where(r=>r.Id==id);
            int count = regions.Count();
            if (count <= 0)
            {
                return null;
            }
            else if (count == 1)
            {
                return ToDTO(regions.Single());
            }
            else
            {
                throw new ApplicationException("获取到多个 Id 为" + id + "的区域信息");
            }
        }

        private RegionDTO ToDTO(Region entity)
        {
            RegionDTO dto = new RegionDTO();
            dto.CreateDateTime = entity.CreateDateTime;
            dto.Id = entity.Id;
            dto.Name = entity.Name;
            dto.CityId = entity.CityId;
            dto.CityName = entity.City.Name;
            return dto;
        }

        public void Add(string name, long cityId)
        {
            Region entity = new Region();
            entity.Name = name;
            entity.CityId = cityId;
            base.Add(entity);
        }
        public void Update(Region entity, string name, long cityId)
        {
            if (entity==null)
            {
                throw new ArgumentException("获取不到区域信息");
            }
            entity.Name = name;
            entity.CityId = cityId;
            base.Update(entity);
        }
    }
}
